<?php
/**
 * User: Jerry
 * Date: 2017/6/20
 * Time: 13:52
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Model\SeminarTemplateBagModel;

/**
 * Class SeminarBagsService
 * @author Jerry
 * @package SDXApi\Service
 */
class SeminarBagsService extends TemplateAbstract
{
    /**
     * @var SeminarTemplateBagModel
     */
    protected $seminarBagModel;

    protected $bonusService;

    /**
     * SeminarBagsService constructor.
     */
    public function __construct()
    {
        $this->seminarBagModel = new SeminarTemplateBagModel();

        $this->bonusService = new BonusService();
    }

    /**
     * @author Jerry
     * @param array $template
     * @param int $userId
     * @param int $wid
     * @return array|bool
     */
    public function handle($template = array(), $userId = 0, $wid = 8)
    {
        return $this->bags($template, $userId);
    }

    /**
     * @author Jerry
     * @param array $template
     * @param int $userId
     * @return array|bool
     */
    protected function bags($template = array(), $userId = 0)
    {
        $bags = $this->getBags($template['id']);

        if (empty($bags)) {
            return false;
        }

        $time = gmtime();

        foreach ($bags as $key => $value) {
            if ($value['send_end_date'] < $time) {
                unset($bags[$key]);
                continue;
            }

            $bags[$key] = $this->checkBagNumber($value, $userId);
        }
        $bags = array_merge($bags);

        $bags = $this->checkDataSort($bags);

        $template['bag_data'] = $bags;

        unset($template['font_type'], $template['font_size'], $template['font_content'], $template['font_link'], $template['image_url'], $template['image_link'], $template['image_size'], $template['goods_type'], $template['goods_id']);

        return $template;
    }

    /**
     * @author Jerry
     * @param array $data
     * @param int $userId
     * @return array
     */
    protected function checkBagNumber($data = array(), $userId = 0)
    {
        $data['use_start_date'] = date('Y-m-d', $data['use_start_date']);
        $data['use_end_date'] = date('Y-m-d', $data['use_end_date']);

        $bonusNumber = $this->bonusService->getBonusNumber($data['bag_id']);
        $data = array_merge($data, $bonusNumber);

        $data['state'] = $this->getBagState($data, $userId);

        $time = gmtime();

        $data['surplus_time'] = 0;
        if ($data['send_start_date'] > $time) {
            $data['surplus_time'] = ($data['send_start_date'] - $time);
        }

        $data['send_start_date'] = localtotime($data['send_start_date']);
        $data['send_end_date'] = localtotime($data['send_end_date']);

        return $data;
    }

    /**
     * @author Jerry
     * @param array $data
     * @param int $userId
     * @return int
     */
    protected function getBagState($data = array(), $userId = 0)
    {

//        未领取数量
        $bagNumber = $this->getBagNumber($data['bag_id']);
//        判断用户是否已领取
        $bagUser = $this->getBagIsUser($userId, $data['bag_id']);
        $bagUserNumber = $this->getBagUserNumber($data['bag_id']);

        $data['state'] = 0;

//        抢完了
        if (($bagNumber == 0) && ($data['limit_send_num'] != 0) && ($data['send_type'] == 3)) {
            return 1;
        }

        if ($data['remainder_number'] == 0) {
            return 1;
        }


        if (($data['send_type'] == 0) && ($data['limit_send_num'] != 0)) {
            if (($data['limit_send_num'] - $bagUserNumber) == 0) {
                return 1;
            }
        }

//        已领过
        if ($bagUser > 0) {
            return 2;
        }

//        未开始
        if ($data['send_start_date'] > gmtime()) {
            return 3;
        }

//        已结束
        if ($data['send_end_date'] < gmtime()) {
            return 4;
        }

        return 0;
    }

    /**
     * @author Jerry
     * @param int $bag_id
     * @return mixed
     */
    protected function getBagNumber($bag_id = 0)
    {
        return $this->seminarBagModel->findBagNotSendNumber($bag_id);
    }

    /**
     * @author Jerry
     * @param int $userId
     * @param int $bag_id
     * @return mixed
     */
    protected function getBagIsUser($userId = 0, $bag_id = 0)
    {
        return $this->seminarBagModel->findBagIsUser($userId, $bag_id);
    }

    /**
     * @author Jerry
     * @param int $bag_id
     * @return mixed
     */
    protected function getBagUserNumber($bag_id = 0)
    {
        return $this->seminarBagModel->findBagUserNumber($bag_id);
    }

    /**
     * @author Jerry
     * @param int $templateId
     * @return mixed
     */
    protected function getBags($templateId = 0)
    {
        $cacheName = 'seminar_template_page_bags_' . $templateId;
        $data = $this->getCache($cacheName);

        if (empty($data)) {
            $data = $this->seminarBagModel->findSeminarTemplateBagsByTemplateId($templateId);

            if (! empty($data)) {
                $this->cacheData($cacheName, $data, mt_rand(3600, 4200));
            }
        }
        return $data;
    }


}